package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.service.base.JwtHelper;
import com.atguigu.guli.service.result.R;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/api/trade/order")
public class ApiOrderController {
    @Autowired
    OrderService orderService;

    //判断是否购买过视屏,
    @GetMapping("/auth/getOrderVideo/{courseId}")
    public R getOrderVideo(@PathVariable String courseId, HttpServletRequest request) {
        // 获取登录者的id
        String memberId = JwtHelper.getId(request);
        // 查询订单表,
        Integer count = orderService.getOrderVideo(memberId, courseId);
        return R.ok().data("isBuy", count > 0 ? "1" : "0");
    }

    //查询订单状态,用于前端跳转
    @GetMapping("/auth/getOrderStatus/{orderId}")
    public R getOrderStatus(@PathVariable String orderId) {
        Order order = orderService.getById(orderId);
        return R.ok().data("status", order.getStatus());

    }

    //查询订单,给前端渲染
    @GetMapping("/auth/orderInfo/{orderId}")
    public R orderInfo(@PathVariable("orderId") String orderId) {
        Order order = orderService.getById(orderId);
        return R.ok().data("item", order);
    }


    //生成某个课程的订单 
    @PostMapping("/auth/getOrder/{courseId}")
    public R creatOrder(HttpServletRequest request, @PathVariable("courseId") String courseId) {
        // 得到客户的id
        String memberId = JwtHelper.getId(request);
        // 生成订单要返回一个订单号给前端
        String orderId = orderService.creatOrder(memberId, courseId);
        return R.ok().data("id", orderId);
    }

}

